Skip to content

perf(compiler): optimize vector arithmetic and numeric loop type inference#2237

Open
ActualMasterOogway wants to merge 3 commits intoluau-lang:masterfrom
ActualMasterOogway:fix-const-folding
Open

perf(compiler): optimize vector arithmetic and numeric loop type inference#2237
ActualMasterOogway wants to merge 3 commits intoluau-lang:masterfrom
ActualMasterOogway:fix-const-folding

Conversation

@ActualMasterOogway
Copy link

@ActualMasterOogway ActualMasterOogway commented Feb 8, 2026

This PR improves the compiler's ability to optimize arithmetic expressions, specifically targeting vector operations and numeric for loops. By improving type inference, we can now apply better optimizations in more places.

Closes #2235

- Relax commutative swap optimization in compileExprBinary to include LBC_TYPE_VECTOR.
- Implement visit(AstStatFor) in TypeMapVisitor to correctly infer numeric type for loop variables.

Fixes luau-lang#2235
@ActualMasterOogway ActualMasterOogway marked this pull request as draft February 8, 2026 16:53
@ActualMasterOogway ActualMasterOogway marked this pull request as ready for review February 8, 2026 17:33
bytecode.emitABC(getBinaryOpArith(expr->op, /* k= */ true), target, rr, uint8_t(lc));

hintTemporaryExprRegType(expr->right, rr, LBC_TYPE_NUMBER, /* instLength */ 1);
hintTemporaryExprRegType(expr->right, rr, *ty, /* instLength */ 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
hintTemporaryExprRegType(expr->right, rr, *ty, /* instLength */ 1);
hintTemporaryExprRegType(expr->right, rr, LBC_TYPE_NUMBER, /* instLength */ 1);

Hint system is used to provide hints on types that are not the expected operands of an instruction.
By saying that 'vector' is expected for an add/mul, hint will not be generated and this will cause jit performance to regress on a wrong type speculation.
For example in this case:

local function foo(t: {a: vector})
    return 2 * t.a
end

With this change, the fast-path is generated for number*number, causing a fallback to be taken at runtime.

Since this was a regression not caught by existing tests, this example should be added as a regression test to IrLowering.test.cpp

Copy link
Collaborator

@vegorov-rbx vegorov-rbx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR says that it is making an improvement with an additional optimization.

While that is true, not a single test case has been added to cover this improvement and show that it is working.
New tests should be placed in Compiler.test.cpp

Copy link
Collaborator

@vegorov-rbx vegorov-rbx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes have to be flagged to be accepted: https://github.com/luau-lang/luau/blob/master/CONTRIBUTING.md#feature-flags

@vegorov-rbx
Copy link
Collaborator

Thank you for providing an improvement. With a few changes it will get to a form we can merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MUL & ADD aren't replaced by their constant counterparts in specific scenarios

2 participants